layui.use(['table', 'from', 'jquery', 'element', 'layer'], function () {
    var form = layui.form;
    var table = layui.table;
    var $ = layui.jquery;
})

function getOrdeList() {
    layui.use(['table','layer'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var layero = layui.layero;
        /*加载表格*/
        console.log(111)
        table.render({
            elem: '#orderList'
            , toolbar: true
            , url: basePath + '/order/orderList'
            , page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
                , groups: 3
                , limits: [20, 50, 100]
                , limit: 20
            },cols: [[
                  {field: 'qid', title: 'ID',width:50, align:'center'}
                , {field: 'image', title: '商品图', templet: '<div><img src="{{d.commodity.image}}" class="layui-nav-img"></div>',width:80 ,align: 'center'}
                , {field: 'commname', title: '商品名称', width:150, align: 'center',templet:'<P>{{d.commodity.commname}}</P>'}
                , {field: 'aliTradeNo', title: '支付宝支付凭证', width: 100, align: 'center'}
                , {field: 'aliPayment', title: '支付时间', width: 80, sort:true, align: 'center'}
                , {field: 'aliPayAmount', title: '支付金额',sort:true, width: 80, align: 'center'}
                , {title: '取件人',width: 80, align: 'center',templet: '<p>{{d.addressInfo.name}}</p>'}
                , {title: '联系电话',width: 100, align: 'center',templet: '<p>{{d.addressInfo.phone}}</p>'}
                , {title: '收货地址',width: 200, align: 'center',templet: '<p>{{d.addressInfo.province}}/{{d.addressInfo.town}}/{{d.addressInfo.county}}/{{d.addressInfo.detail}}</p>'}
                , {title: '操作', width: 200,align: 'center',rowspan: 2,fixed: 'right',toolbar: '#operate'}
            ]], done: function (res, curr, count) {
                var i=1;
                $("[data-field='qid']").children().each(function () {
                    if($(this).text() == 'ID') {
                        $(this).text("ID")
                    }else{
                        $(this).text(i++)
                    }
                });
            },height: 500
        });


        table.on('tool(table)',function (obj){
            var data = obj.data;
            if (obj.event ==='pay'){
                window.open( basePath + '/alipay/pay?subject='+data.commodity.commname+'&traceNo='+data.orderid+'&totalAmount='+data.commodity.thinkmoney)
                layer.open({
                    content: '是否支付成功？',
                    yes: function(index, layero){

                        $.ajax({
                            url:basePath+"/notice/toUser/"+data.commodity.commid+"/"+data.commodity.userid
                            ,data:''
                            ,contentType:'application/json;charset=UTF-8'
                            ,type:'get'
                            ,success:function () {
                                window.location.reload();
                                layer.close(index); //如果设定了yes回调，需进行手工关闭
                            }
                        })

                    },channel:function (){
                        return;
                    }
                });

            }
        })
    })

}

new Vue({
    return:{

    },
})
$(function (){
    $('#btnSearch').click(function (){
        search($("#search").val())
    })
})
getOrdeList();

function search(orderid){
    console.log(orderid);
    layui.use(['jquery','table'],function (){
        var $ = layui.jquery;
        var table = layui.table;
        table.render({
            elem: '#orderList'
            , toolbar: true
            , url: basePath + '/order/getOne/' +orderid
            , page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
                , groups: 3
                , limits: [20, 50, 100]
                , limit: 20
            },cols: [[
                {field: 'qid', title: 'ID',width:50, align:'center'}
                , {field: 'image', title: '商品图', templet: '<div><img src="{{d.commodity.image}}" class="layui-nav-img"></div>',width:80 ,align: 'center'}
                , {field: 'commname', title: '商品名称', width:150, align: 'center',templet:'<P>{{d.commodity.commname}}</P>'}
                , {field: 'aliTradeNo', title: '支付宝支付凭证', width: 100, align: 'center'}
                , {field: 'aliPayment', title: '支付时间', width: 80, sort:true, align: 'center'}
                , {field: 'aliPayAmount', title: '支付金额',sort:true, width: 80, align: 'center'}
                , {title: '取件人',width: 80, align: 'center',templet: '<p>{{d.addressInfo.name}}</p>'}
                , {title: '联系电话',width: 100, align: 'center',templet: '<p>{{d.addressInfo.phone}}</p>'}
                , {title: '收货地址',width: 200, align: 'center',templet: '<p>{{d.addressInfo.province}}/{{d.addressInfo.town}}/{{d.addressInfo.county}}/{{d.addressInfo.detail}}</p>'}
                , {title: '操作', width: 200,align: 'center',rowspan: 2,fixed: 'right',toolbar: '#operate'}
            ]], done: function (res, curr, count) {
                var i=1;
                $("[data-field='qid']").children().each(function () {
                    if($(this).text() == 'ID') {
                        $(this).text("ID")
                    }else{
                        $(this).text(i++)
                    }
                });
            },height: 500
        });



    })
}